#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        printf("invalid numbers!\n");
        return -1;
    }
    int _socket = socket(AF_INET, SOCK_DGRAM, 0);
    if(_socket < 0)
    {
        perror("socket:");
        return -1;
    }
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr. sin_addr.s_addr = inet_addr(argv[1]);
    
    // while(1)
    // {
    //     char temp[100] = {0};
    //     scanf("%[^\n]", temp);
    //     while(getchar()!= '\n');
        // if(sendto(_socket, temp, 100, 0, (struct sockaddr*)&addr, sizeof(addr)) < 0)
        // {
        //     perror("sendto:");
        // }

    // }

   if(bind (_socket, (struct sockaddr*)&addr, sizeof(addr)) != 0)
   {
        perror("blind:");
   }

   while(1)
   {
        char buf[100] = {0};
        struct sockaddr_in clientaddr;
        socklen_t len;
        if(recvfrom(_socket, buf, 100, 0, (struct sockaddr*)&clientaddr, &len) < 0)
        {
            perror("recvfrom:");
        }
        printf("ip: %s   port:% d\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
        printf("content: %s\n", buf);
   }
   close(_socket);
    
    return 0;
}